import {
  Injectable,
  ConflictException,
  NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Website } from './entities/website.entity';
import { WebsiteDto } from './dto/website.dto';

@Injectable()
export class WebsiteService {
  constructor(
    @InjectRepository(Website) private websiteRepository: Repository<Website>,
  ) {}
/**
 * 
 * @param createWebsiteDto 创建网站
 * @returns 
 */
  async create(createWebsiteDto: WebsiteDto): Promise<Website> {
      const product = this.websiteRepository.create(createWebsiteDto);
      return this.websiteRepository.save(product);
    }
  /**
   * 
   * @param id 网站id
   * @returns 
   */
  async findOrcreated(id: number): Promise<Website> {
    const website = await this.websiteRepository.findOne({ where: { id } }); // 使用对象形式查找
    if (!website) {
        const newWebsite = new Website();
        newWebsite.id = id;
        return await this.websiteRepository.save(newWebsite);
    }
    return website;
  }
}
